package cn.rdq.iknessplatform.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.crypto.SecureUtil;
import cn.rdq.api.po.Catelog;
import cn.rdq.api.po.User;
import cn.rdq.api.vo.LoginUser;
import cn.rdq.common.constant.CatelogDefault;
import cn.rdq.common.constant.UserConstant;
import cn.rdq.common.util.Md5Util;
import cn.rdq.common.util.RedisUtil;
import cn.rdq.dao.UserMapper;
import cn.rdq.iknessplatform.service.CatelogService;
import cn.rdq.iknessplatform.service.UserService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
@Transactional(rollbackFor = Exception.class)
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Resource
    private UserMapper userMapper;

    @Autowired
    private CatelogService catelogService;

    @Autowired
    private RedisUtil redisUtil;

    @Override
    public List<User> listUser() {
        return userMapper.selectList(null);
    }

    @Override
    public User tokenUser(String token) {
        Object o = redisUtil.get(token);
        return (User) o;
    }

    @Override
    public User loginByUsernameAndPassword(LoginUser loginUser) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.select("id", "username", "avatar");
        wrapper.eq("username", loginUser.getUsername())
                .eq("password", Md5Util.encodeByMD5(loginUser.getPassword()));
        return getOne(wrapper);
    }

    @Override
    public User register(User user) {
        user.setCreateTime(new Date());
        user.setUpdateTime(new Date());
        user.setAvatar(UserConstant.AVATAR);
        user.setPassword(Md5Util.encodeByMD5(user.getPassword()));
        boolean b = save(user);
        User registerUser = getByUsername(user.getUsername());

        //创建默认清单
        Catelog catelog = new Catelog();
        catelog.setCover(CatelogDefault.COVER);
        catelog.setTitle(CatelogDefault.TITLE);
        catelog.setDescription(CatelogDefault.DESCRIPTION);
        catelog.setUserId(user.getId());
        catelogService.save(catelog);
        return registerUser;
    }

    @Override
    public User getByUsername(String username) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("username", username);
        return getOne(wrapper);
    }

    @Override
    public User getRegister() {
        return userMapper.getRegister();
    }

    @Override
    public void updateUser(User user) {
        UpdateWrapper<User> wrapper = new UpdateWrapper<>();
        if (StringUtils.isNotEmpty(user.getNickname())) {
            wrapper.set("nickname", user.getNickname());
        }
        if (StringUtils.isNotBlank(user.getPassword())) {
            wrapper.set("password", SecureUtil.md5(user.getPassword()).toUpperCase());
        }
        wrapper.set("avatar", user.getAvatar());
        wrapper.set("description", user.getDescription());
        wrapper.set("update_time", new Date());
        wrapper.eq("id", user.getId());
        update(wrapper);
    }
}
